<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\RechargeService;
use Hyperf\Di\Annotation\Inject;

#[AutoController]
class DyPayController extends BaseController
{
    #[Inject]
    private RechargeService $rechargeService;
    #通用支付回调
    public function check_signature(RequestInterface $request, ResponseInterface $response){
        # 验证token问题
//        $applet_id = $request->input("applet_id");
        print_r('----------------消息推送');
        print_r($request->all());
        #从订单中获取小程序token
//        $version = $request->input("version");
        $msg = $request->input("msg");
//        $type =  $request->input("type");
//        $nonce =  $request->input("nonce");
//        $timestamp =  $request->input("timestamp");
        $msg = json_decode($msg,true);
//        $order_id = $msg['out_order_no'];
        if($msg['status'] == 'SUCCESS') {
            $this->rechargeService->buyback($msg,2);
        } else {
            var_dump('取消');
        }
        $res = [];
        $res['err_no'] = 0;
        $res['err_tips'] = 'success';
        return json_encode($res);
    }

    #担保支付
//    public function check_signature(RequestInterface $request, ResponseInterface $response){
//        # 验证token问题
////        $applet_id = $request->input("applet_id");
//        $all = $request->all();
//        print_r('----------------消息推送');
//        print_r($request->all());
//
//
//        #从订单中获取小程序token
//        $signature = $request->input("msg_signature");
//        $timestamp = $request->input("timestamp");
//        $nonce =  $request->input("nonce");
//        $msg =  $request->input("msg");
//        $msgs =  $request->input("msg");
//        $msg = json_decode($msg,true);
//        $order_id = $msg['cp_orderno'];
//
//
//
//        $get_applet_id = $this->rechargeService->get_applet_id($order_id);
//        $res = [];
//        $res['err_no'] = 0;
//        $res['err_tips'] = 'success';
//        $res = json_encode($res);
//        if($get_applet_id) {
//            $applet_id = "$get_applet_id->applet_id";
//        } else {
//            var_dump('获取不到小程序ID');
//            return json_encode($res);
//        }
//        $applet_info = $this->rechargeService->get_applet($applet_id);
//        $token = $applet_info['token'];
//        $sortedString = array();
//        array_push($sortedString, $token);
//        array_push($sortedString, $timestamp);
//        array_push($sortedString, $nonce);
//        array_push($sortedString, $msgs);
//        sort($sortedString);
//        $tmpArr = array($token, $timestamp, $nonce,$msgs);
//        sort($tmpArr, SORT_STRING);
//        $tmpStr = implode( $tmpArr );
//        $tmpStr = sha1( $tmpStr );
////        $concatString = implode("", $sortedString);
////        $hash = sha1($concatString);
////
////        var_dump($hash);
////
////        $tmpStr = sprintf('%x', $hash);
//        var_dump($tmpStr);
//        var_dump($signature);
//
//        if ($tmpStr == $signature ) {
//            if($msg['status'] == 'SUCCESS') {
//                $this->rechargeService->buyback($msg,2);
//            }
//            echo "true".PHP_EOL;
//            return json_encode($res);
//
//        } else {
//            echo "false".PHP_EOL;
//            return json_encode($res);
//        }
//    }


}